package com.camartsApp.controller;

import com.camartsApp.service.UserRelationshipService;
import com.camartsApp.Util.JwtUtils;
import com.camartsApp.Util.Response;
import com.camartsApp.common.ResultVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/relationship")
public class UserRelationshipController {

    @Autowired
    private UserRelationshipService userService;

    @Autowired
    private JwtUtils jwtUtils;

    // 从请求头中获取当前用户ID（从Token中解析）
    private Long getCurrentUserId(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null && authHeader.startsWith("Bearer ")) {
            String token = authHeader.substring(7);
            return jwtUtils.getUserIdFromToken(token);
        }
        return null;
    }

    @GetMapping("/test_test")
    public String test_First() {
        System.out.println("test_First");
        return "test_First";
    }

    /**
     * 关注用户
     * 示例URL: /relationship/123/follow
     */
    @PostMapping("/api/{userId}/follow")
    public ResultVO follow(@PathVariable("userId") Long followingId, HttpServletRequest request) {
        Long followerId = getCurrentUserId(request);
        if (Objects.isNull(followerId)) {
            return Response.FailResultByMsg("无效的Token或用户未登录");

        }
        return userService.follow(followerId, followingId);
    }

    /**
     * 取消关注用户
     * 示例URL: /relationship/123/unfollow
     */
    @PostMapping("/api/{userId}/unfollow")
    public ResultVO unfollow(@PathVariable("userId") Long followingId, HttpServletRequest request) {
        Long followerId = getCurrentUserId(request);
        if (Objects.isNull(followerId)) {
            return Response.FailResultByMsg("无效的Token或用户未登录");
        }
        return userService.unfollow(followerId, followingId);
    }
    @GetMapping("/api/following")
    public ResultVO getFollowingList(HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        if (userId == null) {
            return Response.FailResultByMsg("无效的Token或用户未登录");
        }
        return userService.getFollowingListByUserId(userId);
    }

    /**
     * 获取当前用户的粉丝列表（返回粉丝的用户ID列表）
     */
    @GetMapping("/api/followers")
    public ResultVO getFollowerList(HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        if (userId == null) {
            return Response.FailResultByMsg("无效的Token或用户未登录");
        }
        return userService.getFollowerListByUserId(userId);
    }



    /**
     * 获取指定用户的关注列表（不依赖 Token）
     */
    @GetMapping("/{userId}/following")
    public ResultVO getFollowingList(@PathVariable("userId") Long userId) {
        if (userId == null || userId <= 0) {
            return Response.FailResultByMsg("无效的用户ID");
        }
        return userService.getFollowingListByUserId(userId);
    }

    /**
     * 获取指定用户的粉丝列表（不依赖 Token）
     */
    @GetMapping("/{userId}/followers")
    public ResultVO getFollowerList(@PathVariable("userId") Long userId) {
        if (userId == null || userId <= 0) {
            return Response.FailResultByMsg("无效的用户ID");
        }
        return userService.getFollowerListByUserId(userId);
    }



    /**
     * 判断当前用户是否已关注该用户
     */
    @GetMapping("/api/{targetUserId}/is-following")
    public ResultVO checkIsFollowing(@PathVariable("targetUserId") Long targetUserId,
                                     HttpServletRequest request) {
        Long currentUserId = getCurrentUserId(request);
        if (currentUserId == null) {
            return Response.FailResultByMsg("无效的Token或用户未登录");
        }
        return userService.checkIsFollowing(currentUserId, targetUserId);
    }



}
